/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.andersen.database.dao;

import com.andersen.database.entity.BuyingEntity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;

/**
 *
 * @author Corvin
 */
@Component
public class BuyingDao extends AbstractDao{
    
    private BuyingEntity mapResultSetRow(ResultSet rSet) throws SQLException {
         BuyingEntity result = new BuyingEntity();
         
         result.setId(rSet.getLong("id"));
         result.setPrice(rSet.getLong("price"));
         result.setFirmPricePart(rSet.getLong("firm_price_part"));
         result.setOwnerId(rSet.getLong("owner_id"));
         result.setDescription(rSet.getString("description"));
         result.setMonthPaySum(rSet.getLong("month_pay_sum"));
         
         return result;
    }
    
    public BuyingEntity getById(long id) {
        
        String sql = "SELECT * FROM buying WHERE id = ?;";
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        
        BuyingEntity result = null;
        
        try{

            connection = dataSource.getConnection();
            statement = connection.prepareStatement(sql.toString());
            statement.setLong(1, id);
            resultSet = statement.executeQuery();
            if(resultSet.first())
                result = mapResultSetRow(resultSet);
        } catch(SQLException exc) {
            LOG.error("Error BuyingDao.getById()", exc);
        } finally {
            safeClose(resultSet);
            safeClose(statement);
            safeClose(connection);
        }
        
        return result;
    }
    
    public long insert(BuyingEntity buying) {
        
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO buying(");
        sql.append("price, ");
        sql.append("month_pay_sum, ");
        sql.append("firm_price_part, ");
        sql.append("owner_id, ");
        sql.append("description) VALUE(?,?,?,?,?);");
                
        long resultId = ID_INVALID;
        
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        
        try {
            connection = dataSource.getConnection();
            preparedStatement = connection.prepareStatement(sql.toString(),PreparedStatement.RETURN_GENERATED_KEYS);
            preparedStatement.setLong(1, buying.getPrice());
            preparedStatement.setLong(2, buying.getMonthPaySum());
            preparedStatement.setLong(3, buying.getFirmPricePart());
            preparedStatement.setLong(4, buying.getOwnerId());
            if (buying.getDescription() != null && !buying.getDescription().isEmpty())
                preparedStatement.setString(5, buying.getDescription());
            else preparedStatement.setNull(5, java.sql.Types.NULL);
            if (preparedStatement.executeUpdate() > 0) {
                resultSet = preparedStatement.getGeneratedKeys();
                resultSet.first();
                resultId = resultSet.getLong(1);
            }
        
        } catch (SQLException exc) {
            LOG.error("BuyingDao.insert() error", exc);
        } finally {
            safeClose(resultSet);
            safeClose(preparedStatement);
            safeClose(connection);
        }
        
        
        return resultId;
        
    }
    
    public boolean update(BuyingEntity buying) {
        boolean ok = false;

        StringBuilder sql = new StringBuilder();

        sql.append("UPDATE buying ");
            sql.append("SET price = ?, ");
            sql.append("month_pay_sum = ?, ");
            sql.append("firm_price_part = ?, ");
            sql.append("owner_id = ?, ");
            sql.append("description = ?, ");
        sql.append("WHERE id = ?;");

        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        try {
            conn = dataSource.getConnection();
            st = conn.prepareStatement(sql.toString());
            st.setLong(1, buying.getPrice());
            st.setLong(2, buying.getMonthPaySum());
            st.setLong(3, buying.getFirmPricePart());
            st.setLong(4, buying.getOwnerId());
            if (buying.getDescription() != null && !buying.getDescription().isEmpty())
                st.setString(5, buying.getDescription());
            else st.setNull(5, java.sql.Types.NULL);
            if (st.executeUpdate() == 0) {
                LOG.error("Error with BuyingDao.update() - no rows updated.");
            } else {
                ok = true;
            }
        } catch(SQLException exc) {
            LOG.error("Error with BuyingDao.update()", exc);
        } finally {
            safeClose(rs);
            safeClose(st);
            safeClose(conn);
        }
        return ok;
    }
    
    
    
}
